<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/25 0025
 * Time: 9:19
 */
namespace app\adminapi\controller;

use app\adminapi\model\Api;
use app\api\model\JWT;
use think\App;
use think\Controller;
use think\Db;

/**
 * @title 后台增值服务
 * @description 接口说明
 */
class Increase extends Api
{
    /**
     * @title 增值服务
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/increase/index
     * @method POST
     * @param name:page type:int require:1 default: other: desc:页码
     * 
     * @return list:列表@
     * @list id:增值服务id name:名称 is_hidden:1显示2隐藏 addtime:添加时间
     * @return page:总页数
     */
    public function index()
    {
        $user = $this->user;
        $page = input('page')??1;
        $where[] = ['hid','=',$user['hid']];
        $where[] = ['is_del','=',1];
        $list = db::name('increase')->where($where)->order('addtime desc')->page("$page,10")->select();
        $total = db::name('increase')->where($where)->count();
        if($list){
            foreach ($list as $k => $v) {
                $list[$k]['addtime'] = date('Y-m-d H:i:s',$list[$k]['addtime']);
            }
        }
        $data['list'] = $list;
        $data['page'] = ceil($total/10);
        $data['pages'] = 10;
        $data['total'] = $total;

        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 增值服务保存和修改
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/increase/increase_save
     * @method POST
     * @param name:name type:int require:1 default: other: desc:名称
     * @param name:id type:int require:0 default: other: desc:修改id
     * @param name:is_hidden type:int require:1 default: other: desc:1显示2隐藏
     * @param name:type type:int require:1 default: other: desc:1添加2修改
     * 
     */
    public function increase_save()
    {
        $user = $this->user;
        if(!input('name')){
            return $this->ajaxReturn(1,'请输入名称');
        }
        if(!input('is_hidden')){
            return $this->ajaxReturn(1,'请输入名称');
        }
        $type = input('type');
        $id = input('id');
        $up['name'] = input('name');
        if($type == 2){
            $up['is_hidden'] = input('is_hidden');
            db::name('increase')->where('id',$id)->update($up);
        }elseif($type == 1){
            $up['addtime'] = time();
            $up['is_hidden'] = input('is_hidden');
            $up['hid'] = $user['hid'];
            db::name('increase')->insert($up);
        }
        return $this->ajaxReturn(0,'成功');
    }
    /**
     * @title 增值服务修改获取数据
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/increase/increase_edit
     * @method POST
     * @param name:id type:int require:1 default: other: desc:雅间id
     * 
     * @return list:列表@
     * @list id:id name:名称 is_hidden:1显示2隐藏
     */
    public function increase_edit()
    {
        $user = $this->user;
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');
        $elegant = db::name('increase')->where('id',$id)->field('id,name,is_hidden')->find();
        $data['list'] = $elegant;
        return $this->ajaxReturn(0,'成功',$data);
    }
    /**
     * @title 增值服务删除
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/increase/elegant_del
     * @method POST
     * @param name:id type:int require:1 default: other: desc:雅间id
     * 
     */
    public function elegant_del()
    {
        $user = $this->user;
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');
        // $where[] = ['oid','=',$id];
        // $where[] = ['hid','=',$user['hid']];
        // $order = db::name('increase_order')->where($where)->select();

        // $time = date('Y-m-d',time());
        // $where[] = ['eid','=',$id];
        // $where[] = ['hid','=',$user['hid']];

        // $str = explode("-",$time);
        // $where[] = ['year','=',$str[0]];
        // $where[] = ['month','>=',$str[1]];
        // $where[] = ['day','>=',$str[2]];
        // $where[] = ['status','=',1];

        // $order = db::name('zero')->where($where)->select();
        // if($order){
        //     return $this->ajaxReturn(1,'此雅间有未入座的订单不可删除');
        // }
        db::name('increase')->where('id',$id)->update(['is_del'=>2]);
        return $this->ajaxReturn(0,'成功');
    }
    /**
     * @title 增值服务开关
     * @description 接口说明
     * @author 开发者
     * @url /adminapi/increase/increase_change
     * @method POST
     * @param name:id type:int require:1 default: other: desc:id
     * 
     */
    public function increase_change()
    {
        if(!input('id')){
            return $this->ajaxReturn(1,'缺少参数');
        }
        $id = input('id');
        $res  = db::name('increase')->where('id',$id)->find();
        if($res['is_hidden'] == 1){
            $data['is_hidden'] = '2';
        }elseif($res['is_hidden'] == 2){
            $data['is_hidden'] = '1';
        }
        db::name('increase')->where('id',$id)->update($data);
        return $this->ajaxReturn(0,'成功');
    }

}